//
//  File.swift
//  
//
//  Created by xp on 2023/4/3.
//

import UIKit

/**
 * kCGImageSourceTypeIdentifierHint：设置预设的图片格式，格式参照UTType.h
 * kCGImageSourceShouldAllowFloat：如果文件格式支持，是否应将图像作为浮点CGImageRef返回
 * kCGImageSourceShouldCache：是否应以解码形式缓存图像
 * kCGImageSourceCreateThumbnailFromImageIfAbsent：如果原图中缩略图不存在，是否根据原图创建缩略图
 * kCGImageSourceCreateThumbnailFromImageAlways：是否始终根据原图创建缩略图，即使原图中存在缩略图
 * kCGImageSourceThumbnailMaxPixelSize：缩略图最大尺寸，CFNumber格式
 * kCGImageSourceCreateThumbnailWithTransform：缩略图是否根据原图像的方向和像素纵横比进行旋转和缩放
 *
 */

public extension UIImageView {
    
    
    func setImageIncremental(source: String, progressBlock: DownloadProgressBlock? = nil, completion: VoidBlock? = nil) {
        
        let options: CFDictionary = [
            kCGImageSourceShouldCache: false
        ] as CFDictionary
        
        ImageDownLoader.default.down(source: source, progressBlock: { receivedSize, totalSize, receivedData in
            progressBlock?(receivedSize, totalSize, receivedData)
            autoreleasepool {
                let data = receivedData as CFData
                let incrementSource = CGImageSourceCreateIncremental(nil)

                CGImageSourceUpdateData(incrementSource, data, receivedSize == totalSize)
                let status = CGImageSourceGetStatus(incrementSource)
                switch status {
                case .statusComplete, .statusIncomplete:
                    if let cgImage = CGImageSourceCreateImageAtIndex(incrementSource, 0, options) {
                        DispatchQueue.main.async {
                            self.image = UIImage(cgImage: cgImage)
                        }
                    }
                default:
                    break
                }
            }
        }, completion: completion)
        
    }
    
    /// 设置缩略图。通过imageIO实现
    /// - Parameters:
    ///   - data:
    ///   - size: 图片size
    ///   - scale: 缩放参数
    func setThumbnailImage(data: Data, size: CGSize, scale: CGFloat = UIScreen.main.scale) {
        /// kCGImageSourceShouldCache: 是否缓存解码图像。
        /// kCGImageSourceCreateThumbnailFromImageAlways: 是否始终创建缩略图图像。
        /// kCGImageSourceShouldCacheImmediately: 图像解码和缓存是否在图像创建时发生。
        /// kCGImageSourceCreateThumbnailWithTransform: 是否旋转和缩放缩略图以匹配图像的方向和纵横比。
        /// kCGImageSourceThumbnailMaxPixelSize: 缩略图的最大宽度和高度，以像素为单位指定。
        let imageSourceOptions = [kCGImageSourceShouldCache: false] as CFDictionary
        let imageSource = CGImageSourceCreateWithData(data as CFData, imageSourceOptions)!;
        let maxDimensionInPixels = max(size.width, size.height) * scale
        let downsampleOptions =
        [kCGImageSourceCreateThumbnailFromImageAlways: true,
        kCGImageSourceShouldCacheImmediately: true,
        kCGImageSourceCreateThumbnailWithTransform: true,
        kCGImageSourceThumbnailMaxPixelSize: maxDimensionInPixels] as CFDictionary
        let downsampledImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, downsampleOptions)!
        let hkImage = UIImage(cgImage: downsampledImage)
        self.image = hkImage
    }
    
}
